package com.aegis.demo;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aegis.sdk.signature.model.SignatureParams;
import com.aegis.sdk.signature.utils.SignatureUtil;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author wuweixin
 *
 * @Version 1.0
 * @Descritube 演示摘要签名访问接口
 */
public class DemoSignatureMain1 {

    /**
     * 演示用的，如果appId和secret和你使用的不一样，请根据文档到平台上再重新配置新的
     */
    public static final String APP_ID = "71rv7uk9bdgtbnwop80sh7wxato";
    public static final String APP_SECRET = "9563abdf8c3425f8e8490704cded8ce3206a7a66193ef31f3c6a1602ba57105f";

    /**
     * 这个路径指向的是管理平台的一个demo接口，可以根据情况进行调整
     * 一定要保证这个接口地址是网关的，并且是被网关负载的
     */
    public static final String BASE_URL = "http://localhost:8700/demo2/app";

    /**
     * 下面四个都是接口的请求头
     */
    private static final String HEAD_APP_ID = "X-AE-App-ID";

    private static final String HEAD_TIMESTAMP = "X-AE-Timestamp";

    private static final String HEAD_NONCE = "X-AE-Nonce";

    private static final String HEAD_SIGNATURE = "X-AE-Signature";

    /**
     * 摘要签名演示例子1
     * 演示get请求
     */
    @Test
    public void signatureDemo1() {
        Map<String, String> queryMap = new HashMap<>();
        queryMap.put("name", "张三");
        queryMap.put("age", "18");

        JSONObject body = new JSONObject();
        body.set("content1", "这是测试内容1");
        body.set("content2", "这是测试内容2");

        SignatureParams signatureParams = new SignatureParams
                .Builder()
                .appId(APP_ID)
                //只是演示，没有就不写，get请求一般也不需要请求体
                .body(body.toString())
                .queryMap(queryMap)
                .timestamp(System.currentTimeMillis() + "")
                .nonce(UUID.randomUUID().toString())
                .build();
        System.out.println("签名内容：" + JSONUtil.toJsonStr(signatureParams));
        String signature = SignatureUtil.generateSignature(signatureParams, APP_SECRET);
        System.out.println("签名：" + signature);
        HttpRequest request = HttpUtil.createRequest(Method.GET,
                        BASE_URL + "/demo1" + "?" + signatureParams.getQuery())
                .body(signatureParams.getBody())
                .header(HEAD_APP_ID, signatureParams.getAppId())
                .header(HEAD_TIMESTAMP, signatureParams.getTimestamp())
                .header(HEAD_NONCE, signatureParams.getNonce())
                .header(HEAD_SIGNATURE, signature);
        System.out.println("请求的内容: " + request);
        HttpResponse execute = request.execute();
        System.out.println("响应状态码：" + execute.getStatus());
        System.out.println("响应内容：" + execute.body());

    }


    /**
     * 摘要签名演示例子2
     * 演示get请求
     */
    @Test
    public void signatureDemo2() {
        SignatureParams signatureParams = new SignatureParams
                .Builder()
                .appId(APP_ID)
                //query和queryMap二选一填入
                //和例子1的区别是这里直接拼的是字符串，而不是使用queryMap
                //这里可以不需要对key按字典进行升序，api会处理
                .query("name=张三&age=18")
                .timestamp(System.currentTimeMillis() + "")
                .nonce(UUID.randomUUID().toString())
                .build();
        System.out.println("签名内容：" + JSONUtil.toJsonStr(signatureParams));
        String signature = SignatureUtil.generateSignature(signatureParams, APP_SECRET);
        System.out.println("签名：" + signature);
        HttpResponse execute = HttpUtil.createRequest(Method.DELETE,
                        BASE_URL + "/demo1" + "?" + signatureParams.getQuery())
                .header(HEAD_APP_ID, signatureParams.getAppId())
                .header(HEAD_TIMESTAMP, signatureParams.getTimestamp())
                .header(HEAD_NONCE, signatureParams.getNonce())
                .header(HEAD_SIGNATURE, signature)
                .execute();
        System.out.println("响应状态码：" + execute.getStatus());
        System.out.println("响应内容：" + execute.body());
    }

    /**
     * 摘要签名演示post请求
     */
    @Test
    public void signatureDemo3() {
        JSONObject body = new JSONObject();
        body.set("content1", "这是测试内容1");
        body.set("content2", "这是测试内容2");
        SignatureParams signatureParams = new SignatureParams
                .Builder()
                .appId(APP_ID)
                .body(body.toString())
                //有的话就写入，没有的话这里不需要赋值。也可以用queryMap
                .query("name=张三&age=18")
                .timestamp(System.currentTimeMillis() + "")
                .nonce(UUID.randomUUID().toString())
                .build();
        System.out.println("签名内容：" + JSONUtil.toJsonStr(signatureParams));
        String signature = SignatureUtil.generateSignature(signatureParams, APP_SECRET);
        System.out.println("签名：" + signature);
        HttpResponse execute = HttpUtil.createPost(BASE_URL + "/demo2" + "?" + signatureParams.getQuery())
                .body(signatureParams.getBody())
                .header(HEAD_APP_ID, signatureParams.getAppId())
                .header(HEAD_TIMESTAMP, signatureParams.getTimestamp())
                .header(HEAD_NONCE, signatureParams.getNonce())
                .header(HEAD_SIGNATURE, signature)
                .execute();
        System.out.println("响应状态码：" + execute.getStatus());
        System.out.println("响应内容：" + execute.body());
    }

}
